//
//  SDGameFlowLayout.m
//  Sudoku
//
//  Created by wxlan on 16/6/16.
//  Copyright © 2016年 wxlan. All rights reserved.
//

#import "SDGameFlowLayout.h"

@implementation SDGameFlowLayout
- (CGSize)collectionViewContentSize
{
    CGFloat contentHeight = self.collectionView.height;
    NSInteger rowCount =  self.cellCount/ 9 +1;
    CGFloat margin = 10;
    CGFloat collectionWidth = (mScreenWidth-50-30)/6;
    if (mIsPad) {
        collectionWidth = collectionWidth-20;
    }

    CGFloat contentWidth = (rowCount+1) * margin + rowCount * (collectionWidth*3);
    return CGSizeMake(contentWidth, contentHeight);
    
//    return self.collectionView.size;

}
- (void)prepareLayout
{
    [super prepareLayout];
    
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{

    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < self.cellCount; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        [array addObject:attributes];
    }
    return array;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

            CGFloat collectionWidth = (mScreenWidth-50-30)/6;
    if (mIsPad) {
        collectionWidth = collectionWidth-20;
    }
            CGFloat collectionHeight = collectionWidth;
    
            NSInteger index = indexPath.row;
            NSInteger currentPage = index/9 ;
            NSInteger lastNumber = currentPage *9;
            NSInteger row = (index-lastNumber)/3;
            NSInteger column = (index-lastNumber)%3;
            CGFloat cellMargin =0;
            CGFloat x = currentPage * ((collectionWidth+cellMargin)*3 +10)+ column *(collectionWidth+cellMargin) + 10;
            CGFloat y = row * (collectionWidth+cellMargin) + (self.collectionView.height - collectionHeight*3)*0.5;
            attributes.frame = CGRectMake(x, y, collectionWidth, collectionWidth);

    return attributes;

}

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

{
    
    return YES;
    
}
@end
